package com.android.library.image;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import textsvn.mylibrary.R;

/**
 * @author xiaoxing
 * 图片工具
 */
public class GlideUtil {

    /**
     * 加载视频第一帧做封面图
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadVideoImage(Context context, String url, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).frame(1000000).centerCrop();
        options.error(R.mipmap.ic_placeholder_1).placeholder(R.mipmap.ic_placeholder_1);
        Glide.with(imageView)
                .setDefaultRequestOptions(options)
//                .load(Constant.getResourceUrl(url))
                .load(url)
                .into(imageView);
    }

    /**
     * 加载网络图片
     */
    public static void loadRemoteImage(Context context, String url, ImageView imageView) {
        loadUrlImage(context, url, imageView, R.mipmap.ic_placeholder_1, R.mipmap.ic_placeholder_1, null);
    }

    /**
     * 加载四个角不同角度的圆角
     * 注：图片来源url
     */
    public static void loadCircularImage(Context context, String url, ImageView imageView, int defaultImage,
                                         float leftTopRadius, float rightTopRadius, float leftBottomRadius, float rightBottomRadius) {
        RoundTransform roundTransform = new RoundTransform(RoundTransform.CornerType.TOP, leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius);
        loadUrlImage(context, url, imageView, R.mipmap.ic_placeholder_1, R.mipmap.ic_placeholder_1, roundTransform);
    }

    /**
     * 加载四个圆角图片
     */
    public static void loadCircularAllImage(Context context, String url, ImageView imageView, float radius) {
        RoundTransform roundTransform = new RoundTransform(RoundTransform.CornerType.ALL, radius, radius, radius, radius);
        loadUrlImage(context, url, imageView, R.mipmap.ic_placeholder_1, R.mipmap.ic_placeholder_1, roundTransform);
    }

    /**
     * 加载圆形图片
     */
    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        loadUrlImage(context, url, imageView, R.mipmap.ic_placeholder_1, R.mipmap.ic_placeholder_1, new CircleTransform());
    }

    /**
     * 加载带边框圆形图片
     */
    public static void loadCircleImageWithStroke(Context context, String url, ImageView imageView) {
        loadUrlImage(context, url, imageView, R.mipmap.ic_placeholder_1, R.mipmap.ic_placeholder_1, new CircleWithStrokeTransform());
    }

    public static void loadUrlImage(Context context, String url, ImageView imageView, int error, Transformation<Bitmap> transformation) {
        loadUrlImage(context, url, imageView, error, error, transformation);
    }

    /**
     * 总设置方法
     *
     * @param context        上下文
     * @param url            图片地址
     * @param imageView      view
     * @param placeholder    过度图片资源
     * @param error          加载失败图片资源
     * @param transformation 设置图片样式
     */
    public static void loadUrlImage(Context context, String url, ImageView imageView, int placeholder, int error, Transformation<Bitmap> transformation) {
        if (context == null || imageView == null || imageView.getContext() == null) {
            return;
        }
        RequestOptions options = new RequestOptions()
                //过度图片
                .placeholder(placeholder)
                //错误图片
                .error(error)
                //当地址为null
                .fallback(error)
                .skipMemoryCache(true)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        if (transformation != null) {
            GlideApp.with(context).load(url).apply(options).transform(transformation).dontAnimate().into(imageView);
        } else {
            GlideApp.with(context).load(url).apply(options).dontAnimate().into(imageView);
        }
    }

    /*------------------------------------------------------------------------加载本地资源图片方法---------------------------------------------------------------------------------------------*/

    /**
     * 加载本地Res资源
     */
    public static void loadLocalImage(int resId, ImageView imageView) {
        imageView.setImageResource(resId);
    }

    /**
     * 加载四个圆角图片
     */
    public static void loadCircularAllImage(Context context, int resId, ImageView imageView, float radius) {
        RoundTransform roundTransform = new RoundTransform(RoundTransform.CornerType.ALL, radius, radius, radius, radius);
        loadResImage(context, resId, imageView, -1, -1, roundTransform);
    }

    /**
     * 加载四个角不同角度的圆角
     * 注：图片来源,资源id
     */
    public static void loadCircularImage(Context context, int resId, ImageView imageView, RoundTransform.CornerType type,
                                         float leftTopRadius, float rightTopRadius, float leftBottomRadius, float rightBottomRadius) {
        RoundTransform roundTransform = new RoundTransform(type, leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius);
        loadResImage(context, resId, imageView, -1, -1, roundTransform);
    }

    /**
     * 加载圆形图片
     */
    public static void loadCircleImage(Context context, int resId, ImageView imageView) {
        loadResImage(context, resId, imageView, -1, -1, new CircleTransform());
    }

    /**
     * 加载带边框圆形图片
     */
    public static void loadCircleImageWithStroke(Context context, int resId, ImageView imageView) {
        loadResImage(context, resId, imageView, -1, -1, new CircleWithStrokeTransform());

    }


    /**
     * 总设置方法
     *
     * @param context        上下文
     * @param resId          图片资源id
     * @param imageView      view
     * @param placeholder    过度图片资源
     * @param error          加载失败图片资源
     * @param transformation 设置图片样式
     */
    public static void loadResImage(Context context, int resId, ImageView imageView, int placeholder, int error, Transformation<Bitmap> transformation) {
        if (context == null || imageView == null || imageView.getContext() == null) {
            return;
        }
        if (placeholder == -1) {
            placeholder = R.mipmap.default_error_iamge;
        }
        ;
        if (error == -1) {
            error = R.mipmap.default_error_iamge;
        }
        RequestOptions options = new RequestOptions()
                //过度图片
                .placeholder(placeholder)
                //错误图片
                .error(error)
                //当地址为null
                .fallback(error)
                .centerCrop()
                .skipMemoryCache(true)
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.DATA);
        if (transformation != null) {
            GlideApp.with(context).load(resId).apply(options).transform(transformation).into(imageView);
        } else {
            GlideApp.with(context).load(resId).apply(options).into(imageView);
        }
    }

    /**
     * 加载高斯模糊
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadBlurUrl(Context context, String url, ImageView imageView) {
        GlideApp.with(context)
                .load(url)
                .transform(new BlurTransformation(context, 15))
                .into(imageView);
    }

    /**
     * 加载高斯模糊
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadBlurUrl(Context context, int url, ImageView imageView) {
        GlideApp.with(context)
                .load(url)
                .transform(new BlurTransformation(context, 15))
                .into(imageView);
    }

    public static boolean isDestroy(Activity mActivity) {
        if (mActivity == null || mActivity.isFinishing() || mActivity.isDestroyed()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 当activity销毁前，取消请求
     *
     * @param context
     */
    public static void pauseRequest(Context context) {
        GlideApp.with(context).pauseRequests();
    }

    /**
     * 继续加载
     *
     * @param context
     */
    public static void resumeRequests(Context context) {
        GlideApp.with(context).resumeRequests();
    }
}
